/* people-type : {친구,친구요청받음,아무사이아님}*/

(function(window,undefined){
	var People = {};
	window.People = window.People || People;
	
	var peopleView = People.peopleView = {};
	var peoplePage = People.peoplePage = {};
	peopleView.peopleInfo = function(){
		peopleInfo();
		Talk.loadMyTalkList();
		peopleViewListener();
	};
	
	var peopleInfo = function() {
		var _people_id = getUrlId();
		var _user_id = sessionStorage.getItem('user_id');
		$.ajax({
			type : "post",
			url : ajaxURL + "account.php?action=peopleInfo",
			dataType : "json",
			data : {
				people_id : _people_id,
				user_id : _user_id
			},
			success : function(result) {
				console.log("peopleInfo");
				console.log(result);
				
				$.each(result,function(){
					if(this.image==null){
						this.image='images/default_user.png';
					}
					if(this.point==null){
						this.point=0;
					}
					if(this.description == ''||this.description == null){
						this.description = "한 줄 소개가 없습니다.";
					}
					if(this.isFriend==TRUE){
						$('.peoplepageTitle a').addClass('cancel');
					}
					if(_people_id==_user_id){
						$('.peoplepageTitle a').remove();
					}
					$('#loginWrap img').attr('src',this.image);
					$('#loginWrap .infoWrap a').html(this.name);
					$('#loginWrap .infoWrap ul li:nth(0)').html('GP <b>'+this.point+'</b>');
					$('#loginWrap .infoWrap ul li:nth(1)').html('리뷰 <b>'+this.talkCount+'</b>개');
					$('#loginWrap .infoWrap ul li:nth(2)').html('유용해요 <b>'+this.usefulCount+'</b>번');
					$('#loginWrap .infoLink ul li:nth(0) b').html(this.couponCount);
					$('#loginWrap .infoLink ul li:nth(1) b').html(this.favoriteCount);
					$('#loginWrap .infoLink ul li:nth(2) b').html(this.friendCount);
					$('#loginWrap .introWrap').html('<b><span>'+this.name+'</span>의 한 줄 소개</b>'+this.description);

				});
				$('.addFriend').off('click').on('click',function(event){
					event.preventDefault();
					
					var _from_user_id = _user_id;
					var _to_user_id = _people_id;
					
					if(!_from_user_id){
						alert(NEED_LOGIN_MESSAGE);
						location.href="login.php";
						return;
					}
					
					if($(this).hasClass('cancel')){
						//삭제
						alert(DELETE_FRIEND);
						delFriend(_from_user_id,_to_user_id);
						$(this).removeClass('cancel');
					} else{
						//추가
						alert(REQUEST_FRIEND);
						requestFriend(_from_user_id,_to_user_id);
						$(this).addClass('wating');
					}
				});
			}		
			
		});
	};
	
	var peopleViewListener = function() {
		var _user_id = getUrlId();
		
		$('#peopleTalk').off('click').on('click',function(event){
			event.preventDefault();
			$('section.left').empty();
			$('section.left').html('<h2><img src="images/titles/my_talk.png" alt="cast_list"></h2><div class="clear"></div><ul class="talkList unitWrap styleB"></ul><div id="pagination"><ul></ul><div class="clear"></div></div>');
			Talk.loadMyTalkList();
		});
		$('#peopleStore,#peopleStore0').off('click').on('click',function(event){
			event.preventDefault();
			$('section.left').empty();
			$('section.left').prepend('<h2><img src="images/titles/my_store.png" alt="title_frequenter"></h2><div class="clear"></div><ul class="storeList unitWrap styleA"></ul><div id="pagination"><ul></ul><div class="clear"></div></div>');
			Store.myStoreBlock();
		});
		$('#peopleFriends,#peopleFriends0').off('click').on('click',function(event){
			event.preventDefault();
			$('section.left').empty();
			$('section.left').prepend('<h2><img src="images/titles/my_friend.png" alt="title_guppiifriend"></h2><div class="clear"></div><ul class="peopleList unitWrap styleB"></ul><div class="clear"></div><div id="pagination"><ul></ul><div class="clear"></div></div>');
			People.myFriendBlock();
		});
	};
	
	/* people.php > 피플리스트*/
	People.loadPeopleList = function(){
		var _user_id = sessionStorage.getItem('user_id');
		
		$.ajax({
			type:"post",
			url:ajaxURL + "people.php?action=loadPeopleList",
			dataType:"json",
			data:{user_id:_user_id,row:Paging.getCurrent()},
			success:function(result){
				console.log('loadPeopleList');
				console.log(result);
				$('ul.peopleList').empty();
				
				var count = Paging.getCurrent()+1;
				$.each(result.data,function(){
					if(this.image == null){
						this.image = "images/default_user.png"
					}
					if(this.point == null){
						this.point = 0;
					}
					if(this.description == ''||this.description == null){
						this.description = "한 줄 소개가 없습니다.";
					}
					if(!this.friendType){
						this.friendType = 'addFriend';
					} else if(this.friendType == 'friend'){
						this.friendType = 'addFriend cancel';
					}else if(this.friendType == 'stanby'){
						this.friendType = 'addFriend inventBtn';
					}else if(this.friendType == 'request'){
						this.friendType = 'addFriend wating';
					}
					this.count = count;
					template.peopleList(this,"allPeople");
					
					count++;
				});
				
				$('.addFriend').off('click').on('click',function(event){
					event.preventDefault();
					
					var _from_user_id = sessionStorage.getItem('user_id');
					var _to_user_id = $(this).data('user_id');
					
					if(!_from_user_id){
						if(confirm('본 기능은 로그인이 필요한 기능입니다. 로그인하러 가시겠습니까?')){
							location.href='login.php';
						}
						return ;	
					}
					
					if($(this).hasClass('cancel')){
						//삭제
						alert(DELETE_FRIEND);
						delFriend(_from_user_id,_to_user_id);
						$(this).removeClass('cancel');
					} else if($(this).hasClass('inventBtn')){
						//수락
						alert(ADD_FRIEND);
						acceptFriend(_from_user_id,_to_user_id);
						
						$(this).removeClass('inventBtn');
						$(this).addClass('cancel');
					}else{
						//추가
						alert(REQUEST_FRIEND);
						requestFriend(_from_user_id,_to_user_id);
						$(this).addClass('wating');
					}
				});
				
				var _para = {
					dataLength : result.length,
					// dataLength : 1000,
					selector : '#pagination > ul',
					callback : function(){
						People.loadPeopleList();
					}
				};
				Paging.innerAJAX(_para);
			}
		});
	};
	
	/* myfriends.php > 구피친구*/
	People.myFriendBlock = function(){
		myFriendList('friend');
		$('#tab1').off('click').on("click",function(){
			Paging.initCurrentPage();
			myFriendList('friend');
			$(this).addClass('orange');
			$('#tab2').removeClass('orange');
		});
		$('#tab2').off('click').on("click",function(){
			Paging.initCurrentPage();
			myFriendList('stanby');
			$(this).addClass('orange');
			$('#tab1').removeClass('orange');
		});
	};
	
	var myFriendList = function(_type){
		var _user_id = checkUrlId();
		$.ajax({
			type:"post",
			url:ajaxURL + "people.php?action=loadMyFriendList",
			dataType:"json",
			data:{user_id:_user_id,type:_type,row:Paging.getCurrent()},
			success:function(result){
				console.log('myFriendBlock');
				console.log(result);
				$('ul.peopleList').empty();
				
				if(result.length == 0){
					template.noMyFriend();
					$('.peopleList').css('width','100%');
				}
				var count = Paging.getCurrent()+1;
				
				$.each(result.data,function(){
					$('.peopleList').css('width','700px');
					if(this.image == null){
						this.image = "images/default_user.png"
					}
					if(this.point == null){
						this.point = 0;
					}
					if(this.description == ''||this.description == null){
						this.description = "한 줄 소개가 없습니다.";
					}
					if(!this.friendType){
						this.friendType = 'addFriend';
					} else if(this.friendType == 'friend'){
						this.friendType = 'addFriend cancel';
					}else if(this.friendType == 'stanby'){
						this.friendType = 'addFriend inventBtn';
					}
					this.count = count;
					template.peopleList(this);
					
					count++;
				});
				
				$('.addFriend').off('click').on('click',function(event){
					event.preventDefault();
					
					var _from_user_id = sessionStorage.getItem('user_id');
					var _to_user_id = $(this).data('user_id');
					
					if(!_from_user_id){
						if(confirm('본 기능은 로그인이 필요한 기능입니다. 로그인하러 가시겠습니까?')){
							location.href='login.php';
						}
						return ;	
					}
					
					if($(this).hasClass('cancel')){
						//삭제
						alert(DELETE_FRIEND);
						delFriend(_from_user_id,_to_user_id);
						myFriendList('friend');
						
					} else if($(this).hasClass('inventBtn')){
						//수락
						alert(ADD_FRIEND);
						acceptFriend(_from_user_id,_to_user_id);
						
						myFriendList('stanby');
					}
				});
				$('div.countWrap').html('(<b>'+result.length+'</b>개)');

				var _para = {
					dataLength : result.length,
					// dataLength : 1000,
					selector : '#pagination > ul',
					callback : function(){
						var selectTab=$$('ul.headFilter a.orange').html();
						myFriendList(selectTab);
					}
				};
				Paging.innerAJAX(_para);
			}
		});
	};
	
	var requestFriend = function(__from_user_id,__to_user_id){
		$.ajax({
			type:"post",
			url:ajaxURL + "people.php?action=requestFriend",
			data:{from_user_id:__from_user_id,to_user_id:__to_user_id},
			success:function(result){
				console.log('requestFriend');
				console.log(result);
			}
		});
	};
	
	var acceptFriend = function(__from_user_id,__to_user_id){	
		$.ajax({
			type:"post",
			url:ajaxURL + "people.php?action=acceptFriend",
			data:{from_user_id:__from_user_id,to_user_id:__to_user_id},
			success:function(result){
				console.log('acceptFriend');
				console.log(result);
				
			}
		});
	};
	
	var delFriend = function(__from_user_id,__to_user_id){
		$.ajax({
			type:"post",
			url:ajaxURL + "people.php?action=delFriend",
			data:{from_user_id:__from_user_id,to_user_id:__to_user_id},
			success:function(result){
				console.log('delFriend');
				console.log(result);
			}
		});
	};
	
	/* 현재 친구 상태 확인하기*/
	People.isFriend = function(){
		$.ajax({
			type:"post",
			url:ajaxURL + "people.php?action=",
			data:{},
			success:function(result){
				console.log('isFriend');
				console.log(result);
			}
		});
	};
	
	// var UIButtonToggle = function(selector){
		// if(selector.hasClass('cancel')){
			// selector.removeClass('cancel');
		// } else{
			// selector.addClass('cancel');
		// }
	// };
	
	var template = {
		/*people*/
		peopleList : function(result,type) {
			var info = '';
			info += '<li>';
			info += '<div class="thumbWrap">';
			info += '<a href="people_view.php?id='+result.user_id+'">';
			info += '<img src="' + result.image + '" alt="thumb_store_1" style="width:60px; height:60px;">';
			info += '</a>';
			info += '</div>';
			if(type=="allPeople"){
				info += '<span class="ranking ">' + result.count + '</span>';
			}
			info += '<div class="infoWrap">';
			info += '<a class="title" href="people_view.php?id='+result.user_id+'">' + result.name + '</a>';
			info += '<div class="values">';
			info += '활동지수 <b>' + result.point + '</b><br/> 친구  <b>'+result.getFriendCount+'</b>';
			info += '</div>';
			if(type=="allPeople"||window.location.href.indexOf('?id=')==-1){
				info += '<a href="#" id="friendBtn" class="'+result.friendType+'" data-user_id="'+result.user_id+'">친구수락</a>';
			}
			info += '</div>';
			info += '<div class="clear"></div><div class="commentWrap">';
			info += '<a><b><span>'+result.name+'</span>의 한 줄 소개</b> '+result.description+'</a>';
			info += '</div>';
			info += '</li>';
			
            $('ul.peopleList').append(info);		
		},
		noMyFriend : function(){
			var info = '';
			info += '<div class="noneListWrap">';
			info += '<div class="titleArea">';
			info += '<img src="images/nolist_friend.png" alt=""  />';
			info += '<h4>구피 친구가 없습니다.</h4></div>';
			if(checkUrlId()==sessionStorage.getItem('user_id')){
				info += '<div class="subArea">';
				info += '<p>피플 메뉴에서 다양한 친구들을 만나보세요.<br/><br/>';
				info += '<a href="people.php"><img src="images/btn_link.png" alt="바로가기" /></a>';
				info += '</p></div>';	
			}
			info += '</div>';	
			 $('ul.peopleList').append(info);	
		}
	};
})(window);
